package com.caishi.lkx.user.model;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.caishi.lkx.user.ienum.type.GroupLogRuleTimeType;
import com.caishi.lkx.user.ienum.type.UserLogSourceType;
import com.zzw.common.entity.impl.BaseIntEntity;

import com.caishi.lkx.user.userlog.model.UserLogType;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "sys_group_log_rule", autoResultMap = true)
@Schema(title = "机构日志规则")
public class GroupLogRuleModel extends BaseIntEntity<GroupLogRuleModel> {

    private Long groupId;

    private String name;

    @Schema(title = "任务类型 1:未来 2:修改过往(平均分配) 3：修改过往(随机分配)")
    private GroupLogRuleTimeType timeType;

    @Schema(title = "作用用户id")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<String> userIds;

    @Schema(title = "作用ip")
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private List<String> ips;

    @Schema(title = "作用平台类型")
    @TableField(typeHandler = JacksonTypeHandler.class)
    private List<UserLogSourceType> sourceTypes;

    /**
     * 时间段模式 0 一段 1每年 2每月 3每日 4每时 5每分
     */
    private Integer timeMode;

    /**
     * 结束时间
     */
    private String start;

    /**
     * 开始时间
     */
    private String end;

    /**
     * 对于各种统计的注水倍数/固定值数据
     * 精确到行业
     */
    @TableField(typeHandler = JacksonTypeHandler.class)
    private Map<UserLogType, Map<String/* all表示通用行业 */, String/*0_1,1_1 0 倍数 1固定值*/>> rule;

    /**
     * 1 等待运行
     * 2 运行中
     * 3 已完成
     * 4 已取消
     * 5 暂停中
     */
    private Integer status;

    @TableField(exist = false)
    private String groupName;

    public void setSourceTypes(List<?> sourceTypes) {
        if (sourceTypes == null) {
            this.sourceTypes = null;
            return;
        }
        if (sourceTypes.stream().anyMatch(v -> v instanceof UserLogSourceType)) {
            this.sourceTypes = sourceTypes.stream().map(v -> (UserLogSourceType) v).toList();
        }
        this.sourceTypes = sourceTypes.stream().map(v -> UserLogSourceType.valueOf(v.toString())).toList();
    }

    public void setRule(Map<?, Map<String, String>> rule) {
        if (rule == null) {
            this.rule = null;
            return;
        }
        Map<UserLogType, Map<String, String>> result = new HashMap<>();
        rule.forEach((k, v) -> {
            if (k instanceof UserLogType u) {
                result.put(u, v);
            } else {
                result.put(UserLogType.valueOf(k.toString()), v);
            }
        });
        this.rule = result;
    }

    @Override
    public int hashCode() {
        return getId().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof GroupLogRuleModel gr) return gr.getId().equals(this.getId());
        return super.equals(obj);
    }
}
